package com.blog.util;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;

public class DataSourceUtil {

	public static C3p0Plugin createC3p0Plugin() {
		//根据开发模式配置是否使用加密
		String password = PropKit.get("password");
		if(!PropKit.getBoolean("devMode")){
			if(StringUtils.isEmpty(password)){
				password = "";
			}else{
				try {
					password = ConfigTools.decrypt(password);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"),password);
	}

	public static DruidPlugin createDruidPlugin() {
		//根据开发模式配置是否使用加密
		String password = PropKit.get("password");
		if(!PropKit.getBoolean("devMode")){
			if(StringUtils.isEmpty(password)){
				password = "";
			}else{
				try {
					password = ConfigTools.decrypt(password);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"),PropKit.get("user"), password);
		dp.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType("mysql");
		dp.addFilter(wall);
		return dp;
	}
}
